qx.Class.define("caraffa.widgets.ABMAgenda",
{
	extend : qx.ui.core.Widget,
	construct : function (medicos)
	{
		this.base(arguments);

		if (medicos == null)
			medicos = false;
			
		this._medicos = medicos;
		
		this._setLayout(new qx.ui.layout.Basic());
		
		this._add(this._getAgenda(), {left:0, top:0});
		this._add(this._getGrid(), {left:0, top:0});

		this._cmbMedicos.setEnabled(this._medicos);		
		this._cmbMedicos.addListener("changeValue", this._getDataToGrid, this);
		this._cmbClinica.addListener("changeValue", this._getDataToGrid, this);
		
		this._initForm();
	},
	members :
	{
		_initForm : function ()
		{
			this._grpABMAgenda.setEnabled(false);
			this._grpABMAgenda.hide();
			this._grpGrid.setEnabled(true);
			this._grpGrid.show();
			
			this._chkDias.setValues(["", "", "", "", "", "", ""]);
			
			this._grpHoras.setValue(false);
			this._txtHoraDesde.setValue("");
			this._txtHoraHasta.setValue("");
			
			this._grpFechas.setValue(false);
			this._txdFechaDesde.setValue("");
			this._txdFechaHasta.setValue("");
			
			this._txaObservaciones.setValue("");
			
			this._cmbTipo.setValue("E");
			
			this._btnGuardar.setUserData("cod_agenda", "");
			
			this._getDataToGrid();
		},
		_getDataToGrid : function ()
		{
			if(this._cmbClinica.getValue() != ""){
				this._btnNuevo.setEnabled(true);
			}
			else{
				this._btnNuevo.setEnabled(false);
			}
			
			if (this._cmbMedicos.getValue() != "")
			{
				this._grpOpciones.setEnabled(true);
				var rpc = new qx.io.remote.Rpc();
				rpc.setTimeout(10000);
				rpc.setUrl("services/");
		        rpc.setServiceName("ABMAgenda");
		        try
		        {
		        	var params = new Object();
		        	params.cod_clinica = this._cmbClinica.getValue();
		        	params.medico = this._cmbMedicos.getValue();
					var result = rpc.callSync("getDataToAgenda", params);
					this._grid.getTableModel().setData(result);
				}
				catch (ex)
				{
					alert(ex);
				}
			}
			else
			{
				this._grid.getTableModel().removeRows(0,(this._grid.getTableModel().getRowCount()));
				this._grpOpciones.setEnabled(false);
			}
		},
		_getAgenda : function ()
		{
			this._grpABMAgenda = new qx.ui.groupbox.GroupBox();
			this._grpABMAgenda.setLayout(new qx.ui.layout.Basic());
			
			this._chkDias = new qx.ui.form.ow.Checks("Dias:", [{value:"1", label:"Lunes"}, {value:"2", label:"Martes"}, {value:"3", label:"Miercoles"}, {value:"4", label:"Jueves"}, {value:"5", label:"Viernes"}, {value:"6", label:"Sabado"}, {value:"7", label:"Domingo"}])
	
			this._grpHoras = new qx.ui.groupbox.CheckGroupBox("Horas:");
				this._grpHoras.setLayout(new qx.ui.layout.HBox(10));
				this._grpHoras.setValue(false);
			this._txtHoraDesde = new qx.ui.form.ow.TextField("Desde:");
				this._txtHoraDesde.getLabel().setWidth(50);
				this._txtHoraDesde.getText().setWidth(50);
			this._txtHoraHasta = new qx.ui.form.ow.TextField("Hasta:");
				this._txtHoraHasta.getLabel().setWidth(50);
				this._txtHoraHasta.getText().setWidth(50);
			this._grpHoras.add(this._txtHoraDesde);
			this._grpHoras.add(this._txtHoraHasta);
	
			this._grpFechas = new qx.ui.groupbox.CheckGroupBox("Fechas:");
				this._grpFechas.setLayout(new qx.ui.layout.VBox(5));
				this._grpFechas.setValue(false);
			this._txdFechaDesde = new qx.ui.form.ow.DateText("Desde:");
				this._txdFechaDesde.getLabel().setWidth(80);
				this._txdFechaDesde.getDateText().setWidth(130);
			this._txdFechaHasta = new qx.ui.form.ow.DateText("Hasta:");
				this._txdFechaHasta.getLabel().setWidth(80);
				this._txdFechaHasta.getDateText().setWidth(130);
			this._grpFechas.add(this._txdFechaDesde);
			this._grpFechas.add(this._txdFechaHasta);
			
			this._txaObservaciones = new qx.ui.form.ow.TextArea("Observaciones:");
				this._txaObservaciones.getText().setWidth(355);
				this._txaObservaciones.getText().setHeight(80);
			
			this._cmbTipo = new qx.ui.form.ow.ComboBox("Tipo Recordatorio:", [{value:"L", label:"Dias Laborales", selected:true}, {value:"N", label:"Dias No Laborales"}]);
//				cmbTipo.getLabel()
			
			this._grpABMAgenda.add(this._chkDias, {left:0, top:0});
			this._grpABMAgenda.add(this._grpHoras, {left:120, top:0});
			this._grpABMAgenda.add(this._grpFechas, {left:120, top:70});
			this._grpABMAgenda.add(this._txaObservaciones, {left:0, top:165});
			this._grpABMAgenda.add(this._cmbTipo, {left:20, top:270});
			
			this._grpAcciones = new qx.ui.groupbox.GroupBox();
				this._grpAcciones.setLayout(new qx.ui.layout.HBox(20).set({alignX:"center"}));
				this._grpAcciones.setWidth(355);
			this._btnGuardar = new qx.ui.form.Button("Guardar");
			this._btnCancelar = new qx.ui.form.Button("Cancelar");
			this._grpAcciones.add(this._btnGuardar);
			this._grpAcciones.add(this._btnCancelar);
	
			this._grpABMAgenda.add(this._grpAcciones, {left:0, top:300});
			
			this._eventAcciones();
			
			return this._grpABMAgenda;
		},
		_getGrid : function ()
		{
			this._grpGrid = new qx.ui.groupbox.GroupBox();
				this._grpGrid.setLayout(new qx.ui.layout.VBox(10));
			
			var lyrClinicaMedico = new qx.ui.container.Composite(new qx.ui.layout.HBox(20));
				
			this._cmbClinica = new qx.ui.form.ow.ComboBox("Clinica:", [{value:"1", label:"ZIPOLI"}, {value:"2", label:"AVELLANEDA"}],true);
			this._cmbClinica.getLabel().setWidth(60);
			this._cmbClinica.getCombo().setWidth(100);
			
			this._cmbMedicos = new qx.ui.form.ow.ComboFilter("Medico:", "", this._medicos);
			this._cmbMedicos.getLabel().setWidth(60);
			this._cmbMedicos.getCombo().setWidth(250);
			
			lyrClinicaMedico.add(this._cmbClinica);
			lyrClinicaMedico.add(this._cmbMedicos);
			
			var rpc = new qx.io.remote.Rpc().set({timeout:10000, url:"services/", serviceName:"ABMAgenda"});
			try {
				var params = new Object();
				params.medicos = this._medicos;
				var result = rpc.callSync("getMedicos", params);
				this._cmbMedicos.setNewValues(result, this._medicos);
			}
			catch (ex) {
				alert(ex);
			}
				
			var model = new qx.ui.table.model.Simple();
				model.setColumns(["cod_agenda", "Lu", "Ma", "Mi", "Ju", "Vi", "Sa", "Do", "Hs D.", "Hs H.", "Fecha D.", "Fecha H.", "Tipo"]);
//				model.setData([["X", "X", "X", "X", "X", "X", "X", "88:88", "88:88", "88/88/2009", "88/88/2009"], ["X", "X", "X", "X", "X", "X", "X", "88:88", "88:88", "88/88/2009", "88/88/2009"], ["X", "X", "X", "X", "X", "X", "X", "88:88", "88:88", "88/88/2009", "88/88/2009"], ["X", "X", "X", "X", "X", "X", "X", "88:88", "88:88", "88/88/2009", "88/88/2009"], ["X", "X", "X", "X", "X", "X", "X", "88:88", "88:88", "88/88/2009", "88/88/2009"], ["X", "X", "X", "X", "X", "X", "X", "88:88", "88:88", "88/88/2009", "88/88/2009"], ["X", "X", "X", "X", "X", "X", "X", "88:88", "88:88", "88/88/2009", "88/88/2009"], ["X", "X", "X", "X", "X", "X", "X", "88:88", "88:88", "88/88/2009", "88/88/2009"], ["X", "X", "X", "X", "X", "X", "X", "88:88", "88:88", "88/88/2009", "88/88/2009"], ["X", "X", "X", "X", "X", "X", "X", "88:88", "88:88", "88/88/2009", "88/88/2009"], ["X", "X", "X", "X", "X", "X", "X", "88:88", "88:88", "88/88/2009", "88/88/2009"], ["X", "X", "X", "X", "X", "X", "X", "88:88", "88:88", "88/88/2009", "88/88/2009"], ["X", "X", "X", "X", "X", "X", "X", "88:88", "88:88", "88/88/2009", "88/88/2009"], ["X", "X", "X", "X", "X", "X", "X", "88:88", "88:88", "88/88/2009", "88/88/2009"], ["X", "X", "X", "X", "X", "X", "X", "88:88", "88:88", "88/88/2009", "88/88/2009"]]);		
	
			this._grid = new qx.ui.table.Table(model);
				for (var i=1;i<=7;i++) this._grid.setColumnWidth(i, 35);
				this._grid.setColumnWidth(8, 50);
				this._grid.setColumnWidth(9, 50);
				this._grid.setColumnWidth(10, 80);
				this._grid.setColumnWidth(11, 80);
				this._grid.setColumnWidth(12, 50);
				this._grid.setWidth(570);
				this._grid.setHeight(260);
				this._grid.setColumnVisibilityButtonVisible(false);
				this._grid.getTableColumnModel().setColumnVisible(0, false);

			for (var i=1;i<=7;i++)
			{
				this._grid.getTableColumnModel().setDataCellRenderer(i, new qx.ui.table.cellrenderer.Image(19, 16));
			}
			
//			this._grpGrid.add(this._cmbMedicos);
			this._grpGrid.add(lyrClinicaMedico);
			
			this._grpGrid.add(this._grid);
			this._grpOpciones = new qx.ui.groupbox.GroupBox();
				this._grpOpciones.setLayout(new qx.ui.layout.HBox(20).set({alignX:"center"}));
			this._btnNuevo = new qx.ui.form.Button("Nuevo");
			this._btnNuevo.setEnabled(false);
			this._btnModificar = new qx.ui.form.Button("Modificar");
			this._btnBorrar = new qx.ui.form.Button("Borrar");
			this._grpOpciones.add(this._btnNuevo);
			this._grpOpciones.add(this._btnModificar);
			this._grpOpciones.add(this._btnBorrar);
			
			this._grpGrid.add(this._grpOpciones);
			
			this._eventOpciones();
			
			return this._grpGrid;
		},
		_eventAcciones : function ()
		{
			this._btnGuardar.addListener("execute", function ()
			{
				if (this._btnGuardar.getUserData("cod_agenda") != "")
				{
					if (confirm("Desea guardar las modificaciones realizadas?"))
					{
						var rpc = new qx.io.remote.Rpc();
						rpc.setTimeout(10000);
						rpc.setUrl("services/");
				        rpc.setServiceName("ABMAgenda");
				        try
				        {
				        	if (this._grpHoras.getValue())
				        	{
				        		var hD = this._txtHoraDesde.getValue();
				        		var hH = this._txtHoraHasta.getValue();
				        	}
				        	else
				        	{
				        		var hD = "";
				        		var hH = "";
				        	}
				        	
				        	if (this._grpHoras.getValue())
				        	{
				        		var fD = this._txdFechaDesde.getValueToSQL();
				        		var fH = this._txdFechaHasta.getValueToSQL();
				        	}
				        	else
				        	{
				        		var fD = "";
				        		var fH = "";
				        	}
				        	
							var result = rpc.callSync("updateAgenda", this._btnGuardar.getUserData("cod_agenda"), this._chkDias.getValues(), hD, hH, fD, fH, this._txaObservaciones.getValue(), this._cmbTipo.getValue());
							this._initForm();
						}
						catch (ex)
						{
							alert(ex);
						}
					}
				}
				else
				{
					var rpc = new qx.io.remote.Rpc();
					rpc.setTimeout(10000);
					rpc.setUrl("services/");
			        rpc.setServiceName("ABMAgenda");
			        try
			        {
			        	if (this._grpHoras.getValue())
			        	{
			        		var hD = this._txtHoraDesde.getValue();
			        		var hH = this._txtHoraHasta.getValue();
			        	}
			        	else
			        	{
			        		var hD = "";
			        		var hH = "";
			        	}
			        	
			        	if (this._grpFechas.getValue())
			        	{
			        		var fD = this._txdFechaDesde.getValueToSQL();
			        		var fH = this._txdFechaHasta.getValueToSQL();
			        	}
			        	else
			        	{
			        		var fD = "";
			        		var fH = "";
			        	}
			        	
						var result = rpc.callSync("insertAgenda", this._chkDias.getValues(), hD, hH, fD, fH, this._txaObservaciones.getValue(), this._cmbTipo.getValue(), this._cmbMedicos.getValue(), this._cmbClinica.getValue());
						this._initForm();
					}
					catch (ex)
					{
						alert(ex);
					}
				}
			}, this);
			
			this._btnCancelar.addListener("execute", function ()
			{
				this._initForm();
			}, this);
		},
		_eventOpciones : function ()
		{
			this._btnNuevo.addListener("execute", function ()
			{
				this._grpABMAgenda.setEnabled(true);
				this._grpABMAgenda.show();
				this._grpGrid.setEnabled(false);
				this._grpGrid.hide();
			}, this);
			
			this._btnModificar.addListener("execute", function ()
			{
				if (this._grid.getSelectionModel().getSelectedCount())
				{
					var rpc = new qx.io.remote.Rpc();
					rpc.setTimeout(10000);
					rpc.setUrl("services/");
			        rpc.setServiceName("ABMAgenda");
			        try
			        {
						var result = rpc.callSync("modificarAgenda", this._grid.getTableModel().getValue(0, this._grid.getFocusedRow()), this._cmbMedicos.getValue());
						
						this._chkDias.setValues(result.dias);
						if ((result.hD != "") || (result.hH != ""))
						{
							this._grpHoras.setValue(true);
							this._txtHoraDesde.setValue(result.hD);
							this._txtHoraHasta.setValue(result.hH);
						}
												
						if ((result.fD != "") || (result.fH != ""))
						{
							this._grpFechas.setValue(true);
							this._txdFechaDesde.setValue(result.fD);
							this._txdFechaHasta.setValue(result.fH);
						}
						else
						{
							this._grpFechas.setValue(false);
							this._txdFechaDesde.getDateText().getChildControl("textfield").setValue("");
							this._txdFechaHasta.getDateText().getChildControl("textfield").setValue("");
						}
						this._txaObservaciones.setValue(result.observaciones);
						this._cmbTipo.setValue(result.tipo);
						
						this._btnGuardar.setUserData("cod_agenda", result.cod_agenda);
						
						this._grpABMAgenda.setEnabled(true);
						this._grpABMAgenda.show();
						this._grpGrid.setEnabled(false);
						this._grpGrid.hide();
					}
					catch (ex)
					{
						alert(ex);
					}
				}
				else
				{
					alert("Debe seleccionar el registro que desea Modificar");
				}
			}, this);
			
			this._btnBorrar.addListener("execute", function ()
			{
				if (this._grid.getSelectionModel().getSelectedCount())
				{
					if (confirm("Esta seguro que desea eliminar el registro seleccionado?"))
					{
						var rpc = new qx.io.remote.Rpc();
						rpc.setTimeout(10000);
						rpc.setUrl("services/");
				        rpc.setServiceName("ABMAgenda");
				        try
				        {
							var result = rpc.callSync("deleteAgenda", this._grid.getTableModel().getValue(0, this._grid.getFocusedRow()));
							this._initForm();
						}
						catch (ex)
						{
							alert(ex);
						}
					}
				}
				else
				{
					alert("Debe seleccionar el registro que desea Borrar");
				}
			}, this);
		}
	}
});
